﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using AakStudio.Shell.UI.Controls;
using AakStudio.Shell.UI.Showcase.ViewModels.Collection;

using AvalonDock.Layout;
using SkiaSharp;
using Svg.Skia;

namespace AakStudio.Shell.UI.Showcase
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public sealed partial class MainWindow : CustomChromeWindow
    {
        public MainWindow()
        {
            InitializeComponent();

            // 模拟解决方案结构
            var solution = new TreeNode { Name = "MySolution", Icon = "/Icons/icon-folder.png" };

            var project1 = new TreeNode { Name = "Project1", Icon = "/Icons/icon-foundation.png" };
            project1.Children.Add(new TreeNode { Name = "Program.cs", Icon = "/Icons/icon-or-management.png" });
            project1.Children.Add(new TreeNode { Name = "App.config", Icon = "/Icons/icon-or-management.png" });

            var project2 = new TreeNode { Name = "Project2", Icon = "/Icons/icon-foundation.png" };
            project2.Children.Add(new TreeNode { Name = "Class1.cs", Icon = "/Icons/icon-or-management.png" });
            project2.Children.Add(new TreeNode { Name = "Settings.settings", Icon = "/Icons/icon-or-management.png" });

            solution.Children.Add(project1);
            solution.Children.Add(project2);

            SolutionTreeView.Items.Add(solution);
        }
        public static BitmapImage LoadSvg(string svgPath)
        {
            using var svg = new SKSvg();
            svg.Load(svgPath);

            using var bitmap = new SKBitmap(16, 16);
            using var canvas = new SKCanvas(bitmap);
            canvas.Clear(SKColors.Transparent);
            canvas.DrawPicture(svg.Picture);

            using var image = SKImage.FromBitmap(bitmap);
            using var data = image.Encode(SKEncodedImageFormat.Png, 100);

            var bitmapImage = new BitmapImage();
            using var stream = data.AsStream();
            bitmapImage.BeginInit();
            bitmapImage.StreamSource = stream;
            bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
            bitmapImage.EndInit();

            return bitmapImage;
        }
        private bool isCleanValue;
        public class TreeNode
        {
            public string Name { get; set; }
            public string Icon { get; set; }
            public List<TreeNode> Children { get; set; }

            public TreeNode()
            {
                Children = new List<TreeNode>();
            }
        }
        private void DockingManager_ActiveContentChanged(object sender, System.EventArgs e)
        {
            if (!isCleanValue && IsNotToolWell(dockingManager.ActiveContent))
            {
                ClearValue(ActiveGlowBrushProperty);

                mainStatusBar.ClearValue(BackgroundProperty);
                mainStatusBar.ClearValue(ForegroundProperty);

                isCleanValue = true;
            }
            // On Theme Changed
            else if (IsActive && IsNotToolWell(dockingManager.ActiveContent))
            {
                ActiveContentOfDockingManager();
            }
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        private bool IsNotToolWell(object value)
        {
            return value is AakCollectionViewModel || value is AakDocumentWellViewModel;
        }

        private void MetroWindow_Activated(object sender, System.EventArgs e)
        {
            ActiveContentOfDockingManager();
        }

        private void ActiveContentOfDockingManager()
        {
            // if the window is activated, then active the last actived item in docking manager
            var hasFloatingWindow = false;

            var items = dockingManager.Layout.Descendents().OfType<LayoutContent>().ToList();
            for (var i = 0; i < items.Count; i++)
            {
                var item = items[i];
                if (item.IsFloating)
                {
                    if (!hasFloatingWindow)
                        hasFloatingWindow = true;
                    items.RemoveAt(i--);
                }
            }

            if (hasFloatingWindow && items.Count > 0)
            {
                var index = 0;

                var tmpTimeStamp = items[0].LastActivationTimeStamp;
                for (var j = 1; j < items.Count; j++)
                {
                    var item2 = items[j];
                    if (item2.LastActivationTimeStamp > tmpTimeStamp)
                    {
                        tmpTimeStamp = item2.LastActivationTimeStamp;
                        index = j;
                    }
                }

                items[index].IsActive = true;
            }
        }
    }
}